using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO
{
    public class GalleryDAO : Abstracts.AbstractGenericDAO<Gallery, System.Nullable<int>>,
        Interfaces.IGalleryDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "gallery_id";
        }

        /// <summary>
        /// Pobieranie listy obiektow na podstawie ich języka i szablonu.
        /// </summary>
        /// <param name="templateId">szablon</param>
        /// <param name="languageId">język</param>
        /// <returns>lista rekordow o zadanym języku i szablonie</returns>
        public IQueryable<Gallery> FindByTemplateIdAndLanguageId(int templateId, int languageId)
        {
            if (templateId.Equals(0) || languageId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<Gallery> galleries = this._cmsEntitiesDataContext.Galleries.Where(
                    lang_temp => lang_temp.language_id.Equals(languageId) && lang_temp.template_id.Equals(templateId)
                        );
                if (!(galleries.Any()))
                {
                    return null;
                }   //if
                return galleries;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektu na podstawie ich języka i szablonu.
        /// </summary>
        /// <param name="templateId">szablon</param>
        /// <param name="languageId">język</param>
        /// <returns>rekord o zadanym języku i szablonie</returns>
        public Gallery FindByNameAndLanguageId(string name, int languageId)
        {
            if (String.IsNullOrEmpty(name) || languageId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                Gallery gallerie = this._cmsEntitiesDataContext.Galleries.SingleOrDefault(
                    gall => gall.language_id.Equals(languageId) && gall.gallery_name.Equals(name)
                        );
                return gallerie;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie listy obiektow na podstawie ich języka.
        /// </summary>
        /// <param name="languageId">język</param>
        /// <returns>lista rekordow o zadanym języku</returns>
        public IQueryable<Gallery> FindByLanguageId(int languageId)
        {
            if (languageId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<Gallery> galleries = this._cmsEntitiesDataContext.Galleries.Where(
                    lang => lang.language_id.Equals(languageId)
                        );
                if (!(galleries.Any()))
                {
                    return null;
                }   //if
                return galleries;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Wstawianie listy obiektów na podstawie jezyka.
        /// </summary>
        /// <param name="defaultLanguageId">domyslny jezyk</param>
        /// <param name="newLanguageId">nowy jezyk</param>
        /// <returns>true jesli sie udalo, false wpp</returns>
        public bool InsertForNewLanguage(int defaultLanguageId, int newLanguageId)
        {
            if (defaultLanguageId.Equals(0) || newLanguageId.Equals(0))
            {
                return false;
            }   //if
            try
            {
                IQueryable<Gallery> galleries = this.FindByLanguageId(defaultLanguageId);
                TemplateDAO _templateDAO = new TemplateDAO();
                ModuleDAO _moduleDAO = new ModuleDAO();
                if (galleries == null)
                {
                    return true;
                }   //if
                if (!(galleries.Any()))
                {
                    return true;
                }   //if
                foreach (Gallery gall in galleries)
                {
                    Gallery galleryToInsert = new Gallery();
                    Template oldTemp = _templateDAO.FindByPK(gall.template_id);
                    if (oldTemp == null)
                    {
                        return false;
                    }
                    Template newTemp = _templateDAO.FindByLangAndName(newLanguageId, oldTemp.template_name);
                    if (newTemp == null)
                    {
                        return false;
                    }
                    galleryToInsert.module_id = null;
                    if (gall.module_id != null)
                    {
                        Module oldModule = _moduleDAO.FindByPK(gall.module_id);
                        if (oldModule == null)
                        {
                            return false;
                        }
                        Module newModule = _moduleDAO.FindByLanguageIdAndModuleName(newLanguageId, oldModule.module_name);
                        if (newModule == null)
                        {
                            return false;
                        }
                        galleryToInsert.module_id = newModule.module_id;
                    }
                    
                    galleryToInsert.language_id = newLanguageId;
                    galleryToInsert.gallery_alias = gall.gallery_alias;
                    galleryToInsert.gallery_created = DateTime.Now;
                    galleryToInsert.gallery_created_by_id = gall.gallery_created_by_id;
                    galleryToInsert.gallery_modified = DateTime.Now;
                    galleryToInsert.gallery_modified_by_id = gall.gallery_modified_by_id;
                    galleryToInsert.gallery_name = gall.gallery_name;
                    galleryToInsert.gallery_ordering = gall.gallery_ordering;
                    galleryToInsert.gallery_published = gall.gallery_published;

                    galleryToInsert.template_id = newTemp.template_id;
                    this.Add(galleryToInsert);
                }

            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return true;
        }

        /// <summary>
        /// Usuwanie obiektow po jezyku.
        /// </summary>
        /// <param name="languageId">jezyk</param>
        /// <returns>true dla sukcesu, false w przeciwnym razie</returns>
        public bool DeleteByLanguage(int languageId)
        {
            if (languageId.Equals(0))
            {
                return false;
            }   //if
            try
            {
                IQueryable<Gallery> galleries = this.FindByLanguageId(languageId);
                if (galleries == null)
                {
                    return true;
                }   //if
                if (!(galleries.Any()))
                {
                    return true;
                }   //if
                foreach (Gallery gallery in galleries)
                {
                    this._cmsEntitiesDataContext.Galleries.DeleteOnSubmit(gallery);
                }
                try
                {
                    _cmsEntitiesDataContext.SubmitChanges();

                }
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                return true;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }
    }
}
